<template>
  <div class="xiaobu-container" v-closeSelect="'window'">
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      class="search-form clearfix"
    >
      <el-form-item class="search-field fl" label="登录用户名" prop="loginUserName">
        <el-input
          v-model.trim="queryParams.loginUserName"
          placeholder="请输入登录用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="search-field fl" label="登录ip地址" prop="loginIp">
        <el-input
          v-model.trim="queryParams.loginIp"
          placeholder="请输入登录ip地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="search-field fl" label="状态" prop="status">
        <el-select-dict
          dictType="LOGIN_STATUS"
          clearable
          placeholder="请选择状态"
          v-model="queryParams.status"
          @change="handleQuery"
        ></el-select-dict>
      </el-form-item>
      <el-form-item class="search-field fl" label="登录时间" prop="beginTime">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          @change="handleQuery"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item class="fr">
        <el-button plain @click="resetQuery">重 置</el-button>
        <el-button type="primary" @click="handleQuery">查 询</el-button>
      </el-form-item>
    </el-form>
    <div class="table-wrap">
      <div class="table-box">
        <el-table
          :class="{'empty': list.length === 0}" 
          empty-text=" "
          v-loading="loading"
          :data="list"
          row-key="dictId"
        >
          <el-table-column-dict
            maskType="mobile"
            label="登录用户名"
            prop="loginUserName"
            align="center"
            min-width="200px"
          />
          <el-table-column
            label="请求URL"
            prop="apiUrl"
            align="center"
            min-width="300px"
            show-overflow-tooltip
          />
          <el-table-column-dict
            dictType="LOGIN_STATUS"
            dictTypeNode="tag"
            label="状态"
            align="center"
            prop="status"
            min-width="100px"
          ></el-table-column-dict>
          <el-table-column-dict
            dateFormat="datetime"
            label="登录日期"
            align="center"
            prop="loginTime"
            min-width="168px"
          ></el-table-column-dict>
          <el-table-column
            v-if="
              hasPermi([
                'log:login:read',
              ])
            "
            label="操作"
            align="center"
            width="256px"
            fixed="right"
            class-name="small-padding "
          >
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['log:login:read']"
                size="mini"
                type="primary"
                icon="el-icon-view"
                @click.native.stop="getDetail(scope.row)"
                >查看</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>

    <login-log-dialog  
      ref="loginLogDialog"
      title="日志详情"
      width="996px"
      :visible.sync="open"
      :modal-append-to-body="false"
      :close-on-click-modal="false"
      :baseForm="baseForm"
      @close="open = false"
      @successs="getList"></login-log-dialog>
  </div>
</template>

<script>
import { logApi } from "@/api"
import LoginLogDialog from "./components/login-log-dialog.vue"
export default {
  name: "logLoginManage",
  components: {
    LoginLogDialog
  },
  data() {
    return {
      //搜索条件参数
      queryParams: {
        loginUserName: "", // 登录用户名称
        loginIp: "", // 登录ip地址
        status: "", // 登录状态
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10
      },
      dateRange: [],
      list: [],
      loading: false,
      total: 0,
      open: false,
      // 表单参数
      baseForm: {
        loginUserName: "", // 登录用户名
        loginIp: "", // 登录IP地址
        apiUrl: "", // 接口请求地址
        status: "", // 请求状态
        loginMsg: "", // 请求提示语
        loginTime: "", // 登录时间
        responseTime: "", // 接口响应时间
        loginLocationCountry: "", // 登录地址(国家)
        loginLocationCity: "", // 登录地址(城市)
        loginLocationRegion: "", // 登录地址(区域)
        loginLocationLatitude: "", // 登录地址(经度)
        loginLocationLongitude: "", // 登录地址(纬度)
        browser: "" // 登录设备信息
      }
    }
  },
  created() {
    //获取列表
    this.getList()
  },
  methods: {
    /** 搜索按钮登录 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮登录 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true
      logApi
        .getLoginLogList(this.addDateRange(this.queryParams, this.dateRange))
        .then((response) => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        })
        .catch((err) => {
          if (err?.name === "CanceledError") {
            return
          }
          this.loading = false
        })
    },
    /** 查看详情 */
    getDetail(row) {
      const id = row.logId
      logApi.getLoginLogDetailById({ id }).then((data) => {
        this.baseForm = Object.assign(this.baseForm, data)
        this.open = true
      })
    }
  }
}
</script>
